package com.wintrue.ffxs.ui.mine;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.wintrue.ffxs.R;
import com.wintrue.ffxs.base.BaseFragmentActivity;
import com.wintrue.ffxs.ui.mine.adapter.MyFragmentAdapter;
import com.wintrue.ffxs.utils.DensityUtil;
import com.wintrue.ffxs.widget.actionbar.CommonActionBar;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lhe on 2017/8/22.
 */

public class MyOrderListActivity extends BaseFragmentActivity implements ViewPager.OnPageChangeListener{

    @Bind(R.id.common_action_bar)
    CommonActionBar actionBar;
    @Bind(R.id.order_list_all)
    TextView orderListAll;
    @Bind(R.id.order_list_daiwancheng)
    TextView orderListDaiwancheng;
    @Bind(R.id.order_list_yiwancheng)
    TextView orderListYiwancheng;
    @Bind(R.id.order_list_closed)
    TextView orderListClosed;
    @Bind(R.id.order_list_cursor)
    View cursor;
    @Bind(R.id.order_list_viewpager)
    ViewPager viewPager;
    int curPosition = -1;
    int cursorOffset = 0;
    private MyOrderListFrament allFragment;
    private MyOrderListFrament daiwanchengFragment;
    private MyOrderListFrament yiwanchengFragment;
    private MyOrderListFrament closedFragment;
    private ArrayList<MyOrderListFrament> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        actionBar.setActionBarTitle("我的订单");
        actionBar.setLeftImgBtn(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        allFragment = new MyOrderListFrament(this, MyOrderListFrament.ORDER_ALL);
        daiwanchengFragment = new MyOrderListFrament(this, MyOrderListFrament.ORDER_DAIWANCHENG);
        yiwanchengFragment = new MyOrderListFrament(this, MyOrderListFrament.ORDER_YIWANCHENG);
        closedFragment = new MyOrderListFrament(this, MyOrderListFrament.ORDER_CLOSED);
        fragments = new ArrayList<>();
        fragments.add(allFragment);
        fragments.add(daiwanchengFragment);
        fragments.add(yiwanchengFragment);
        fragments.add(closedFragment);
        viewPager.setOnPageChangeListener(this);
        viewPager.setAdapter(new MyFragmentAdapter<>(getSupportFragmentManager(),fragments));
        ViewGroup.LayoutParams lp = cursor.getLayoutParams();
        cursorOffset = DensityUtil.getScreenWidth(this)/fragments.size();
        lp.width = cursorOffset;
        viewPager.setCurrentItem(0);
        switchItem(0);
    }

    @OnClick({R.id.order_list_all,R.id.order_list_daiwancheng,R.id.order_list_yiwancheng,R.id.order_list_closed})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.order_list_all:
                viewPager.setCurrentItem(0);
                break;
            case R.id.order_list_daiwancheng:
                viewPager.setCurrentItem(1);
                break;
            case R.id.order_list_yiwancheng:
                viewPager.setCurrentItem(2);
                break;
            case R.id.order_list_closed:
                viewPager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switchItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void switchItem(int position){
        if(curPosition == position){
            return;
        }
        switch (position){
            case 0:
                orderListAll.setTextColor(getResources().getColor(R.color.color_f23030));
                orderListDaiwancheng.setTextColor(getResources().getColor(R.color.color_545454));
                orderListYiwancheng.setTextColor(getResources().getColor(R.color.color_545454));
                orderListClosed.setTextColor(getResources().getColor(R.color.color_545454));
                break;
            case 1:
                orderListAll.setTextColor(getResources().getColor(R.color.color_545454));
                orderListDaiwancheng.setTextColor(getResources().getColor(R.color.color_f23030));
                orderListYiwancheng.setTextColor(getResources().getColor(R.color.color_545454));
                orderListClosed.setTextColor(getResources().getColor(R.color.color_545454));
                break;
            case 2:
                orderListAll.setTextColor(getResources().getColor(R.color.color_545454));
                orderListDaiwancheng.setTextColor(getResources().getColor(R.color.color_545454));
                orderListYiwancheng.setTextColor(getResources().getColor(R.color.color_f23030));
                orderListClosed.setTextColor(getResources().getColor(R.color.color_545454));
                break;
            case 3:
                orderListAll.setTextColor(getResources().getColor(R.color.color_545454));
                orderListDaiwancheng.setTextColor(getResources().getColor(R.color.color_545454));
                orderListYiwancheng.setTextColor(getResources().getColor(R.color.color_545454));
                orderListClosed.setTextColor(getResources().getColor(R.color.color_f23030));
                break;
        }
        TranslateAnimation animation = new TranslateAnimation(curPosition * cursorOffset,position*cursorOffset,0,0);
        animation.setFillAfter(true);
        animation.setDuration(300);
        cursor.startAnimation(animation);
        MyOrderListFrament orderListFrament = fragments.get(position);
        orderListFrament.notifyData();
        curPosition = position;
    }
}
